 <!--
* description: 维修档案日志
* author: chenjingwen
* createdDate: 2021-3-2
-->
<template>
  <div class="app-container app-container-table">
    <one-table-template
      ref="multipleTable"
      :dynamic-buttons="tableButtons"
      :dynamic-components="tableComponents"
      :dynamic-table-cols="tableCols"
      :dynamic-api-config="apiConfig"
      :dynamic-form-fields="formField"
      :dynamic-is-show-select="true"
      :dynamic-is-show-more-btn="true"
      :callback-obj="callbackObj"
    />
  </div>
</template>
<script>
import { oneTableWithViewTemplateMixins } from '@/components/mixins/oneTableWithViewTemplateMixins'
import OneTableTemplate from '@/components/templates/oneTable'
import api from '@/api/se/electronicarchives'
import Vue from 'vue'

export default {
  name: 'Maintenancefilelog',

  components: {
    OneTableTemplate
  },

  // 组件混入对象：{data[listQuery] methods[queryTable]}
  mixins: [oneTableWithViewTemplateMixins],

  // 阻塞路由预加载网格中组件的数据
  beforeRouteEnter(to, from, next) {
    Vue.prototype.$ConfigCache.CacheConfig.initData([to.path, ''], function() {
      next()
    })
  },

  data() {
    // 页面标识（与上面beforeRouteEnter中的对应）
    const viewPageCode = this.$route.path

    // 绑定事件配置化事件
    this.$ConfigCache.CacheConfig.bindEvent(this, [viewPageCode])

    return {
      // 网格查询API配置对象
      apiConfig: api.queryArchiveLog,

      // 动态组件-按钮
      tableButtons: [
        {
          compKey: 'btnKey1',
          type: 'primary',
          size: 'small',
          name: 'search',
          clickEvent: () => this.queryTable(1),
          text: this.$t('sys.button.query'),
          position: 'right',
          fuzzySearch: true
        },
        {
          compKey: 'btnKey2',
          size: 'small',
          name: 'reset',
          clickEvent: () => this.reset(),
          text: this.$t('sys.button.reset'),
          position: 'right'
        },
        {
          compKey: 'btnKey3',
          size: 'small',
          name: 'reset',
          clickEvent: () => this.repeatSend(),
          text: '重发',
          position: 'right',
          disabled: true
        }

      ],
      // 动态组件-查询条件
      tableComponents:
        this.$ConfigCache.CacheConfig.cacheData[this.$route.path] &&
        this.$ConfigCache.CacheConfig.cacheData[this.$route.path]
          .tableComponents &&
        this.$ConfigCache.CacheConfig.cacheData[this.$route.path]
          .tableComponents.length > 0
          ? this.$ConfigCache.CacheConfig.cacheData[this.$route.path]
            .tableComponents
          : [
            {
              compKey: 'compKey1',
              labelName: '门店编码：',
              codeField: 'dlrCode',
              component: () => import('@/components/pa/salesStore'),
              type: 'inputText',
              isMul: false,
              isMust: true
            },
            {
              compKey: 'compKey2',
              labelName: '发送日期：',
              codeField: 'sendDateStart,sendDateEnd',
              component: () => import('@/components/org/datePicker/datePicker.vue'),
              dateType: 'daterange',
              isMul: false,
              isMust: true
            },
            {
              compKey: 'compKey3',
              labelName: '状态：',
              codeField: 'statusCode',
              component: () => import('@/components/org/LookupValue'),
              oFields: 'statusCode,status',
              options: [
                { statusCode: '1', status: '成功' },
                { statusCode: '2', status: '失败' }
              ],
              type: 'dropdownList',
              isMul: false,
              isMust: true
            },
            {
              compKey: 'compKey4',
              labelName: '派工单号：',
              codeField: 'repairOrderCode',
              component: () => import('@/components/org/commonInput'),
              type: 'inputText',
              isMul: false,
              isMust: true
            }
          ],
      // 动态生成网格列
      tableCols:
        this.$ConfigCache.CacheConfig.cacheData[this.$route.path] &&
        this.$ConfigCache.CacheConfig.cacheData[this.$route.path].tableCols &&
        this.$ConfigCache.CacheConfig.cacheData[this.$route.path].tableCols
          .length > 0
          ? this.$ConfigCache.CacheConfig.cacheData[this.$route.path].tableCols
          : [
            {
              prop: 'orderId',
              label: '主键ID',
              hidden: true,
              alwaysHidden: true
            },
            {
              prop: 'updateControlId',
              label: '并发控制ID',
              hidden: true,
              alwaysHidden: true
            },

            {
              prop: 'dlrCode',
              label: '门店编码'
            },
            {
              prop: 'companyname',
              label: '维修企业名称'
            },
            {
              prop: 'repairOrderCode',
              label: '派工单号'
            },
            {
              prop: 'sendDate',
              label: '发送时间',
              width: 140
            },
            {
              prop: 'status',
              label: '状态'
            },
            {
              prop: 'returnResults',
              label: '返回结果'
            },
            {
              prop: 'vehicleplatenumber',
              label: '车牌号'
            },
            {
              prop: 'vin',
              label: 'VIN码'
            },
            {
              prop: 'repairDate',
              label: '到店时间',
              width: 140
            }

          ],

      // 表单数据
      formField: this.$utils.getFormField(this, {
        dlrCode: '',
        sendDateStart: '',
        sendDateEnd: '',
        statusCode: '',
        repairOrderCode: '',
        orderId: ''
      }, this.$ConfigCache.CacheConfig.cacheData[viewPageCode]),

      // 模版页面组件回调事件
      callbackObj: {
        // 当选择项发生变化时会触发该事件（多选时）
        onCheckSelectChange: (selection) => {
          this.selectData = selection

          this.handleSelectionChange(selection)
        }
      }
    }
  },
  mounted() {
    this.queryTable(1)
  },

  methods: {
    // 选择列是否生效
    getSelectable(row, index) {
      if (row.statusCode === '2') {
        return true // 生效
      }
    },
    // 通过
    repeatSend() {
      // debugger
      this.$confirm('确定要重发吗?', '提示', {
        type: 'warning'
      }).then(() => {
        const queryObj = {
        // 保存需要传 type="mutation"
          type: 'mutation',
          apiConfig: api.retransmission,
          // 条件/实体参数（变量），根据typeParam中的定义配置
          variables: {
            list: this.selectData.reduce((pre, cur, idx) => {
              const { orderId } = cur
              pre.push({ orderId })
              return pre
            }, [])
          }

        }
        // 转换了中台请求格式数据
        var paramD = this.$getParams(queryObj)
        this.$requestAPI(paramD).then((response) => {
          if (response.result == '1') {
            this.$message({
              message: response.msg
            })
          } else {
            this.$message({
              message: response.msg
            })
          }
          this.queryTable(1)
        })
      }).catch(err => {
        console.log(err)
      })
    },

    // 当选择项发生变化时会触发该事件
    handleSelectionChange(selection) {
      const btns = this.tableButtons.filter(o => o.disabled !== undefined)
      if (btns.length > 0) {
        const isCheck = selection && selection.length > 0
        btns.forEach((item) => {
          item.disabled = !isCheck
        })
      }
    }
  }
}
</script>
